const Sequelize = require('sequelize')

module.exports = (sequelize, DataTypes) => {

    class recommendCategory extends Sequelize.Model {

        /**
         * @author: Shihh
         * @description: 排序获取所有推荐
         * @param {type} 
         * @return: 
         */
        static async getRecommend(params) {
            let result = await this.findAll({ where: {status:0}, order: [['sort', 'ASC']] })
            return result;
        }

        /**
         * @author: Shihh
         * @description: 获取推荐分类
         * @param {type} 
         * @return: 
         */
        static async getRecommendCategory(pars) {
            var querydata = []
            var [page = 1, pagesize = 15, limit = 0] = [pars.page, pars.pagesize, pars.limit]
            let condition = {
                attributes: ['id', 'name', 'status', 'sort', 'create_time', 'link_type', 'link_id', 'link', 'recommend_type','link_name'],
                raw: true,
                offset: pagesize * (page - 1),
                limit: parseInt(pagesize)
            }
            querydata = await this.findAll(condition)
            return querydata;
        }

        /**
         * @author: Shihh
         * @description: 新增推荐分类
         * @param {type} 
         * @return: 
         */
        static async addRecommendCategory(form) {
            let create_time = parseInt(new Date().getTime() / 1000);
            return await this.create({
                name: form.name,
                status: form.status,
                sort: form.sort,
                link: form.link,
                link_id: form.link_id == '' ? null : form.link_id,
                link_type: form.link_type,
                recommend_type: form.recommend_type,
                create_time: create_time,
                link_name:form.link_name
            }).then(re => {
                return true
            }).catch(err => {
                console.log(err)
                return false
            })
        }

        /**
         * @author: Shihh
         * @description: 编辑推荐分类
         * @param {type} 
         * @return: 
         */
        static async editRecommendCategory(form) {
            return await this.update({
                name: form.name,
                status: form.status,
                sort: form.sort,
                link: form.link,
                link_id: form.link_id == '' ? null : form.link_id,
                link_type: form.link_type,
                recommend_type: form.recommend_type,
                link_name:form.link_name
            }, {
                'where': {
                    'id': form.id
                }
            }).then(re => {
                return true
            }).catch(err => {
                return false
            })
        }
        /**
         * @author: Shihh
         * @description: 修改专题状态
         * @param {type} 
         * @return: 
         */
        static async modifyCategoryStatus(pars) {
            return await this.update({
                status: pars.status
            }, {
                'where': {
                    'id': pars.id
                }
            }).then(re => {
                return true
            }).catch(err => {
                return false
            })

        }

        /**
         * @author: Shihh
         * @description: 删除
         * @param {type} 
         * @return: 
         */
        static async deleteRecommended(pars) {
            return await this.destroy({
                where: {
                    id: pars.id,
                },
            }).then(re => {
                return true
            }).catch(err => {
                return false
            })
        }
        /**
         * @author: Shihh
         * @description: 获取列表条数，用于添加排序
         * @param {type} 
         * @return: 
         */
        static async getRecommendCount() {
            return this.max('sort');
        }

        // 拽动排序
        /**
         * @author: Shihh
         * @description: 暂不修改，ctrl挪动后处理
         * @param {type} 
         * @return: 
         */
        async recommendSort(params) {
            var queryData = []
            var valueArr = []
            let sql = `select id,sort from recommend_category order by sort asc`
            queryData = await this.raw(sql)
            //start拖动的目的地
            //end从哪一行开始拖动的
            //start《end从后往前拖动   start》end从前往后拖动
            var value = {}
            if (parseInt(params.start) < parseInt(params.end)) { //从后往前拖动
                for (var i = parseInt(params.start); i <= parseInt(params.end); i++) {
                    console.log(i == parseInt(params.end))
                    if (i == parseInt(params.end)) {
                        value = {
                            id: queryData[i].id,
                            sort: parseInt(params.start) + 1
                        };
                    } else {
                        value = {
                            id: queryData[i].id,
                            sort: queryData[i].sort + 1,
                        };
                    }

                    valueArr.push(value)
                }
            } else if (parseInt(params.start) > parseInt(params.end)) { //从前往后拖动
                for (var j = parseInt(params.end); j <= parseInt(params.start); j++) {
                    if (j == parseInt(params.end)) {
                        value = {
                            id: queryData[j].id,
                            sort: parseInt(params.start) + 1
                        }
                    } else {
                        value = {
                            id: queryData[j].id,
                            sort: queryData[j].sort - 1,
                        };
                    }

                    valueArr.push(value)
                }
            }
            var updateData = []
            updateData = await this.bulkCreate(valueArr, {
                updateOnDuplicate: ["sort"]
            });
            return updateData
        }

    }

    recommendCategory.init({
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        name: { type: DataTypes.STRING(45) },
        sort: { type: DataTypes.INTEGER },
        status: { type: DataTypes.INTEGER, defaultValue: 0 },
        link: { type: DataTypes.STRING(255) },
        link_id: { type: DataTypes.INTEGER },
        link_type: { type: DataTypes.STRING(255) },
        create_time: { type: DataTypes.INTEGER },
        recommend_type: { type: DataTypes.INTEGER(6) },
        link_name:{ type: DataTypes.STRING(45) }
    }, {
        sequelize,
        modelName: 'recommend_category',
        freezeTableName: true,
        timestamps: false
    });

    return recommendCategory;
}